Repository navigation
GROOVY-12829: a closure passes a method of Object on for a closure nested in it - #3354
Open
paulk-asert wants to merge 1 commit into
Open
paulk-asert wants to merge 1 commit into
paulk-asert wants to merge 1 commit into
Conversation
…sted in it An implicit call of toString(), hashCode(), equals(), getClass() or an extension method of Object is first tried on a closure's owner and delegate. An owner or delegate that is itself a closure answered the call as an ordinary object. It now applies its own resolution rules, as for a call made in its own body, so the call reaches the enclosing instance or a delegate however deeply the closure is nested. Static compilation resolves such a call the same way, and an enclosing closure with TO_SELF still answers it.
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #3354 +/- ##
==================================================
+ Coverage 74.9518% 74.9564% +0.0047%
- Complexity 48908 48929 +21
==================================================
Files 1768 1768
Lines 161213 161251 +38
Branches 31728 31742 +14
==================================================
+ Hits 120832 120868 +36
+ Misses 28968 28966 -2
- Partials 11413 11417 +4
🚀 New features to boost your workflow:
|
✅ All tests passed ✅🏷️ Commit: 4ff15ab Learn more about TestLens at testlens.app/docs. |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
An implicit call of
toString(),hashCode(),equals,getClass()or anextension method of
Objectin a closure is first tried on the closure'sowner and delegate (GROOVY-11858, GROOVY-12576). When the owner is itself a
closure, that closure answered the call as an ordinary object, so wrapping a
call in another closure changed which object answered. An enclosing closure
now applies its own resolution rules to such a call, as it does for any other
name.
Behaviour
toString()in a nested closure, by version (dynamic;Cis the enclosinginstance,
Da delegate):each/collectin a closurenew D().with { ... }DELEGATE_FIRST/DELEGATE_ONLYclosureTO_SELFor dehydrated closure@TypeCheckedand@CompileStaticcode now agree on every row.Under static compilation only
toString(),hashCode(),equalsandgetClass()change; the GDK methods already reached the enclosing instance.same result as a statically compiled lambda there.
enclosing code. Up to 5.1.3 Groovy matched a Java anonymous class instead;
6.0.0 matched neither.
Unchanged: a closure that is not nested, any other method or property name,
the methods
Closuredeclares,this.toString(),owner.toString(), andtoString()called on a closure from outside it.Changes
ClosureMetaClass.invokeMethod: an owner or delegate that is a generatedclosure is handed the call as for one made in its own body.
StaticTypeCheckingVisitor: the branch that selected the owner closure'sown method is removed, and an enclosing
TO_SELFclosure is added as areceiver for a closure nested in it.
@PackedClosures: a statically compiled closure making such a call in anested closure now packs. The dynamic decline (GROOVY-12663) is kept, with
its comment and message corrected.
every object has, with a new example. The closing note states that
statically compiled code sends a name resolved to the enclosing class to
the
thisobject, where dynamic code asks the owner; the two differ onlyafter
rehydrate.COMPATIBILITY.md: an entry under Groovy 7.Points for review
an owner.
rehydratesentence in the guide's note describes existing behaviourthat is worth agreeing on.
Compatibility
Code that relies on a bare
toString(),getClass(),withand the like ina nested closure reaching the enclosing closure now reaches the enclosing
instance or delegate.
owner.keeps the old result. No public API changes.Tests
ClosuresSTCTest.testClosureObjectMethodResolveStrategyand twoPackedClosuresTransformTestcases pinned the old behaviour and are updated;ClosuresSpecTest.testObjectMethodsInNestedClosuresis new.